home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTLOCK.C < prev    next >
Text File  |  1990-06-20  |  5KB  |  209 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btlock.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /* function declarations */
  13. #ifdef AC_HDRS
  14. int resync(btree_t *btp);
  15. #else
  16. int resync();
  17. #endif
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      btlock - btree lock
  22.  
  23. SYNOPSIS
  24.      #include <btree.h>
  25.  
  26.      int btlock(btp, ltype)
  27.      btree_t *btp;
  28.      int ltype;
  29.  
  30. DESCRIPTION
  31.      The btlock function controls the lock status of a btree.  The btp
  32.      argument is an open btree.  ltype indicates the target status of
  33.      the lock on the btree.
  34.  
  35.      The lock types available are:
  36.  
  37.           BT_UNLCK - unlock btree
  38.           BT_RDLCK - lock btree for reading
  39.           BT_WRLCK - lock btree for reading and writing
  40.           BT_RDLKW - lock btree for reading (wait)
  41.           BT_WRLKW - lock btree for reading and writing (wait)
  42.  
  43.      For the lock types which wait, btlock will not return until the
  44.      lock is available.  For the lock types which do not wait, if
  45.      the lock is unavailable because of a lock held by another process
  46.      a value of -1 is returned and errno set to EAGAIN.
  47.  
  48.      When a btree is unlocked, its cursor is set to null.
  49.  
  50.      The buffers are flushed before unlocking.
  51.  
  52.      btlock will fail if one or more of the following is true:
  53.  
  54.      [EAGAIN]       ltype is BT_RDLCK and btp is already write locked
  55.                     by another process, or ltype is BT_WRLCK and btp
  56.                     is already read or write locked by another
  57.                     process.
  58.      [EINVAL]       btp is is not a valid btree pointer.
  59.      [EINVAL]       ltype is not one of the valid lock types.
  60.      [BTECORRUPT]   btp is corrupt.
  61.      [BTENOPEN]     btp is not open.
  62.      [BTENOPEN]     ltype is BT_RDLCK or BT_RDLKW and btp is not
  63.                     opened for reading or ltype is BT_WRLCK or
  64.                     BT_WRLKW and btp is not open for writing.
  65.  
  66. SEE ALSO
  67.      btgetlck.
  68.  
  69. DIAGNOSTICS
  70.      Upon successful completion, a value of 0 is returned.  Otherwise,
  71.      a value of -1 is returned, and errno set to indicate the error.
  72.  
  73. ------------------------------------------------------------------------------*/
  74. int btlock(btp, ltype)
  75. btree_t *btp;
  76. int ltype;
  77. {
  78.     int bltype = 0;        /* blkio lock type */
  79.  
  80.     /* validate arguments */
  81.     if (!bt_valid(btp)) {
  82.         errno = EINVAL;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if btree not open */
  87.     if (!(btp->flags & BTOPEN)) {
  88.         errno = BTENOPEN;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if btree not open for lock ltype */
  93.     switch (ltype) {
  94.     case BT_UNLCK:
  95.         btp->cbtpos.node = NIL;        /* set cursor to null */
  96.         btp->cbtpos.key = 0;
  97.         bltype = B_UNLCK;
  98.         break;
  99.     case BT_RDLCK:
  100.         if (!(btp->flags & BTREAD)) {
  101.             errno = BTENOPEN;
  102.             return -1;
  103.         }
  104.         bltype = B_RDLCK;
  105.         break;
  106.     case BT_RDLKW:
  107.         if (!(btp->flags & BTREAD)) {
  108.             errno = BTENOPEN;
  109.             return -1;
  110.         }
  111.         bltype = B_RDLKW;
  112.         break;
  113.     case BT_WRLCK:
  114.         if (!(btp->flags & BTWRITE)) {
  115.             errno = BTENOPEN;
  116.             return -1;
  117.         }
  118.         bltype = B_WRLCK;
  119.         break;
  120.     case BT_WRLKW:
  121.         if (!(btp->flags & BTWRITE)) {
  122.             errno = BTENOPEN;
  123.             return -1;
  124.         }
  125.         bltype = B_WRLKW;
  126.         break;
  127.     default:
  128.         errno = EINVAL;
  129.         return -1;
  130.         break;
  131.     }
  132.  
  133.     /* lock btree file */
  134.     if (lockb(btp->bp, bltype, (bpos_t)0, (bpos_t)0) == -1) {
  135.         if (errno != EAGAIN) BTEPRINT;
  136.         return -1;
  137.     }
  138.  
  139.     /* set status bits in btree control structure */
  140.     switch (ltype) {
  141.     case BT_UNLCK:
  142.         btp->flags &= ~BTLOCKS;
  143.         break;
  144.     case BT_RDLCK:
  145.     case BT_RDLKW:
  146.         /* if previously unlocked, re-sync with file */
  147.         if (!(btp->flags & BTLOCKS)) {
  148.             if (resync(btp) == -1) {
  149.                 BTEPRINT;
  150.                 return -1;
  151.             }
  152.         }
  153.         btp->flags |= BTRDLCK;
  154.         btp->flags &= ~BTWRLCK;
  155.         break;
  156.     case BT_WRLCK:
  157.     case BT_WRLKW:
  158.         /* if previously unlocked, re-sync with file */
  159.         if (!(btp->flags & BTLOCKS)) {
  160.             if (resync(btp) == -1) {
  161.                 BTEPRINT;
  162.                 return -1;
  163.             }
  164.         }
  165.         btp->flags |= (BTRDLCK | BTWRLCK);
  166.         break;
  167.     default:
  168.         BTEPRINT;
  169.         errno = BTEPANIC;
  170.         return -1;
  171.         break;
  172.     }
  173.  
  174.     errno = 0;
  175.     return 0;
  176. }
  177.  
  178. /* resync:  re-sync with file */
  179. int resync(btp)
  180. btree_t *btp;
  181. {
  182.     unsigned long    oldheight    = 0;
  183.     btpos_t    *    sp        = NULL;
  184.  
  185.     oldheight = btp->bthdr.height;
  186.     if (bgeth(btp->bp, &btp->bthdr) == -1) {
  187.         BTEPRINT;
  188.         return -1;
  189.     }
  190.     if (btp->bthdr.flags & BTHMOD) {
  191.         errno = BTECORRUPT;
  192.         return -1;
  193.     }
  194.     if (btp->bthdr.height != oldheight) {
  195.         sp = (btpos_t *)realloc(btp->sp, (size_t)(btp->bthdr.height + 1) * sizeof(*sp));
  196.         if (sp == NULL) {
  197.             BTEPRINT;
  198.             errno = ENOMEM;
  199.             return -1;
  200.         }
  201.         btp->sp = sp;
  202.         sp = NULL;
  203.         btp->sp[btp->bthdr.height].node = NIL;
  204.         btp->sp[btp->bthdr.height].key = 0;
  205.     }
  206.  
  207.     return 0;
  208. }
  209.